<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/6/10
 * Time: 15:22
 */
namespace app\api\controller\v2;

use think\Db;
/**
 * @title 文章评论接口
 * @desc
 * @url /api/v2/article_comment
 * @version 2.0.0
 * Class Article
 * @package app\api\controller\v2
 */
class ArticleComment extends Auth
{
    protected $restActionList = ['index','read','save','update','delete'];

    public function index()
    {
        return show(200,'列表成功',[]);
    }

    public function read()
    {
        return show(200,'查看成功成功',[]);
    }

    public function save()
    {
        if(!request()->isPost())
        {
            return show(config('code.error'), '未知操作', '', 403);
        }
        $id = input('post.id/d');
        $content = htmlspecialchars(input('content'));

        // 判断评论内容是否有敏感词汇 #####################################
        if($this->filter_words($content))
        {
            return json(['code'=>12111,'msg'=>'含有敏感词汇!']);
        }
        $comment = Db::table('wechatadmin.system_article_comment')
            ->where(['aid'=>$id,'uid'=>$this->user['id'],'content'=>$content,'status'=>0])->find();

        // 非重复评论  新增评论
        if($comment === NULL)
        {
            if(Db::table('wechatadmin.system_article_comment')
                ->insert(['aid'=>$id,'uid'=>$this->user['id'],'content'=>$content,'create_time'=>time()]))
            {
                Db::table('wechatadmin.system_article')->where('id',$id)->setInc('comment');

                // 返回当前评论的信息放在前台显示
                $comment = Db::table('wechatadmin.system_article_comment')->alias('ac')
                    ->field('ac.id,ac.aid,ac.content,ac.status,ac.create_time,ac.goods,
                    ac.reply,u.nickname,u.avatar')
                    ->join('wechat_backup_07_18_14_43_00.app_user u','u.uc_id=ac.ucid','INNER')
                    ->where(['aid'=>$id,'uid'=>$this->user['id'],'content'=>$content])
                    ->find();

                $comment['is_owner'] = true;
                $comment['good_status'] = false;


                return json(['code'=>200,'msg'=>'Comment success.','data'=>$comment]);
            }
        }
        else
        {
            return json(['code'=>11201,'msg'=>'Do not repeat the comments.']);
        }
        return show(200,'提交成功',[]);
    }

    public function update()
    {
        return show(200,'更新成功',[]);
    }

    public function delete()
    {
        return show(200,'删除成功',[]);
    }
    /**
     * 参数规则
     * @name 字段名称
     * @type 类型
     * @require 是否必须
     * @default 默认值
     * @desc 说明
     * @range 范围
     * @return array
     */
    public static function getRules()
    {
        $rules = [
            'index' => [
                'cid' => ['name' => 'cid', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '栏目id', 'range' => '星文推荐为1',],
                'page' => ['name' => 'page', 'type' => 'int', 'require' => 'false', 'default' => '1', 'desc' => '页码', 'range' => '',],
            ],
            'read' => [
                'id' => ['name' => 'id', 'type' => 'int', 'require' => 'true', 'default' => '无', 'desc' => '文章id', 'range' => '',],
            ],
        ];
        return $rules;
    }
}